from sqlalchemy import Column, Integer, String, Text, JSON, Enum, DateTime, ForeignKey
from sqlalchemy.sql import func
from sqlalchemy.orm import relationship
import enum
from app.core.database import Base

class QuestionType(str, enum.Enum):
    CHOICE = "choice"
    FILL = "fill"
    SOLVE = "solve"

class Question(Base):
    __tablename__ = "questions"

    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    content = Column(Text, nullable=False)
    type = Column(Enum(QuestionType), nullable=False)
    options = Column(JSON, nullable=True)
    correct_answer = Column(Text, nullable=False)
    explanation = Column(Text, nullable=True)
    difficulty = Column(Integer, nullable=False)
    score = Column(Integer, default=5)
    grade_id = Column(Integer, ForeignKey("grades.id"), nullable=False)
    knowledge_point_id = Column(Integer, ForeignKey("knowledge_points.id"), nullable=False)
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    updated_at = Column(DateTime(timezone=True), onupdate=func.now())

    test_papers = relationship("TestPaper", secondary="test_paper_questions", back_populates="questions")
    grade_rel = relationship("Grade", back_populates="questions")
    knowledge_point_rel = relationship("KnowledgePoint", back_populates="questions")